/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
  let length = 0, p
  p = head
  while (p) {
    length++
    p = p.next
  }

  p = head, count = 0
  while (p) {
    if (count != length - n) {
      count++
      p = p.next
    } else {
      p.next = p.next.next
      break
    }

  }

  return head


};